package com.hzy.fivepalace.admin.service;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayFundTransUniTransferModel;
import com.alipay.api.domain.Participant;
import com.alipay.api.request.AlipayFundTransUniTransferRequest;
import com.alipay.api.response.AlipayFundTransUniTransferResponse;
import com.hzy.fivepalace.admin.config.pay.AliPayProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;

/**
 * 支付宝支付服务中心
 *
 * @author gejian
 */
public class AliPayService {

    private static Logger log = LoggerFactory.getLogger(AliPayService.class);


    protected AliPayProperties aliPayProperties;

    /**
     * 支付宝网关地址
     */
    private static final String gatewayUrl = "https://openapi.alipay.com/gateway.do";

    /**
     * 设置配置对象.
     *
     * @param aliPayProperties the config
     */
    public void setConfig(AliPayProperties aliPayProperties) {
        this.aliPayProperties = aliPayProperties;
    }

    /**
     * 获取配置对象
     *
     * @return
     */
    public AliPayProperties getAliPayProperties() {
        return this.aliPayProperties;
    }



    /**
     * 支付宝转账
     * @param bizNo
     * @param amount
     * @param content
     * @param loginAccount
     * @param name
     * @return
     * @throws AlipayApiException
     */
    public AlipayFundTransUniTransferResponse transUniTransfer(String bizNo, BigDecimal amount, String content, String loginAccount, String name) throws AlipayApiException {
        AliPayProperties parameter =this.getAliPayProperties();

        CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
        certAlipayRequest.setServerUrl(gatewayUrl);  //gateway:支付宝网关（固定）
        certAlipayRequest.setAppId(parameter.getAppId());  //APPID 即创建应用后生成,详情见创建应用并获取 APPID
        certAlipayRequest.setPrivateKey(parameter.getPrivateKey());  //开发者应用私钥，由开发者自己不能生成
        certAlipayRequest.setFormat("json");  //参数返回格式，只支持 json 格式
        certAlipayRequest.setCharset("UTF-8");  //请求和签名使用的字符编码格式，支持 GBK和 UTF-8
        certAlipayRequest.setSignType("RSA2");  //商户生成签名字符串所使用的签名算法类型，目前支持 RSA2 和 RSA，推荐商家使用 RSA2。
        certAlipayRequest.setCertPath(parameter.getPublicKeyPath()); //应用公钥证书路径（app_cert_path 文件绝对路径）
        certAlipayRequest.setAlipayPublicCertPath(parameter.getPublicKeyRsa2Path()); //支付宝公钥证书文件路径（alipay_cert_path 文件绝对路径）
        certAlipayRequest.setRootCertPath(parameter.getRootCertPath());  //支付宝CA根证书文件路径（alipay_root_cert_path 文件绝对路径）

        AlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest);
        AlipayFundTransUniTransferRequest request = new AlipayFundTransUniTransferRequest();
        AlipayFundTransUniTransferModel model = new AlipayFundTransUniTransferModel();
        model.setOutBizNo(bizNo);
        model.setTransAmount(amount.toString());
        model.setProductCode("TRANS_ACCOUNT_NO_PWD");
        model.setBizScene("DIRECT_TRANSFER");
        model.setOrderTitle(content);
        model.setRemark(content);
        Participant payeeInfo = new Participant();
        payeeInfo.setIdentity(loginAccount);
        payeeInfo.setIdentityType("ALIPAY_LOGON_ID");
        payeeInfo.setName(name);
        model.setPayeeInfo(payeeInfo);
        request.setBizModel(model);
        return alipayClient.certificateExecute(request);
    }
}
